//
//  YCAchievementController.m
//  YCVillageDoctor
//
//  Created by Mac on 2017/6/7.
//  Copyright © 2017年 zhangxiaoliang. All rights reserved.
//

#import "YCAchievementController.h"
#import "InComeCell.h"
#import "AchievementView.h"
#import "AchievementViewModel.h"

@interface YCAchievementController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,weak)UITableView *myTableView;

@property (nonatomic, strong)AchievementView *headerView;

@property (nonatomic, assign) NSInteger page_num;
//数据源
@property (nonatomic, strong)NSMutableArray<AchievementViewModel *>*achievements;

@end

@implementation YCAchievementController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.page_num  = 1;
    _achievements = [NSMutableArray array];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"我的业绩";
    [self setUpSubViews];
    [self presentLoadingTips:@"正在加载"];
    [self loadData];
    self.automaticallyAdjustsScrollViewInsets = NO;
  
}

//我的业绩
-(void)loadData {
    
     __weak typeof(self)weakSelf = self;
    NSString *url = kHzOrderList;
    NSDictionary *params = @{
                   @"page_num":@(self.page_num).stringValue,
                   @"link_mobile":judgeString([YCUserTool sharedTools].linkMobile)
                             };
    
    [DownloadData post:url params:params success:^(id json) {
   
        if (json&&[json count]>0) {
            [weakSelf.achievements addObjectsFromArray:[AchievementViewModel mj_objectArrayWithKeyValuesArray:json]];
            NSLog(@"%@",json);
            [self.myTableView reloadData];
        }
        if (weakSelf.achievements.count==0) {
             [weakSelf noDataViewShowWithSuperView:self.view andTitle:@"暂无数据"];
        }
        [weakSelf dismissTips];
        [weakSelf.myTableView.mj_header endRefreshing];
        [weakSelf.myTableView.mj_footer endRefreshing];
    } failure:^(NSError *error) {
        [weakSelf dismissTips];
        [weakSelf.myTableView.mj_header endRefreshing];
        [weakSelf.myTableView.mj_footer endRefreshing];
    }];
}

-(void)setInCome:(YCInComeModel *)inCome{
    _inCome = inCome;
    self.headerView.inCome = _inCome;
}

-(void)setUpSubViews {
    
    AchievementView *headerView = [AchievementView achievementViewWithFrame:CGRectMake(0, 10, [UIScreen mainScreen].bounds.size.width, 105)];
    self.headerView = headerView;
    self.headerView.inCome = _inCome;
    [self.view addSubview:self.headerView];
    
    UITableView *tableView = [[UITableView alloc]init];
    
    tableView.delegate = self;
    tableView.dataSource = self;
    
    self.myTableView = tableView;
    
    tableView.estimatedRowHeight = 200;

    tableView.rowHeight = UITableViewAutomaticDimension;
    
    [self.view addSubview:tableView];
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.bottom.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view).offset(115);
    }];
 
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.myTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        self.page_num = 1;
        [self.achievements removeAllObjects];
         [self.myTableView reloadData];
        [self loadData];
        
    }];
    
    self.myTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        self.page_num ++;
        [self loadData];
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.achievements.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    InComeCell *cell = [InComeCell inComeCellWithTabelView:tableView];
    cell.model = self.achievements[indexPath.row];
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
